package jmx.dynamicmbean;

import java.lang.reflect.Constructor;

import javax.management.Attribute;
import javax.management.AttributeChangeNotification;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.DynamicMBean;
import javax.management.InvalidAttributeValueException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;
import javax.management.ReflectionException;

import sample.Util;

public class TomcatServerController extends NotificationBroadcasterSupport implements DynamicMBean {
	private ServerInfo serverInfo;
	private long startTime;

	public TomcatServerController() {
		System.out.println("TomcatServerController Constructor Called ...");
		serverInfo = new ServerInfo("TOMCAT", "7", "APACHE");
	}

	public void startServer(String adminName) throws InterruptedException {
		startTime = System.currentTimeMillis();
		System.out.println("Started at :" + startTime + " by : " + adminName);

		Thread.sleep(1000 * 60);

	}

	// DynamicMBean method implementations

	@Override
	public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException,
			ReflectionException {
		System.out.println("actionName:" + actionName);
		System.out.println("params[0]:" + params[0]);
		for (String s : signature)
			System.out.println("signature#:" + s);

		if ("startServer".equals(actionName))
			try {
				startServer((String) params[0]);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		return "ok";
	}

	@Override
	public MBeanInfo getMBeanInfo() {
		return buildDynamicMBeanInfo();
	}

	private MBeanInfo buildDynamicMBeanInfo() {
		MBeanAttributeInfo[] dAttributes = new MBeanAttributeInfo[1];
		MBeanConstructorInfo[] dConstructors = new MBeanConstructorInfo[1];
		MBeanParameterInfo[] params = { new MBeanParameterInfo("adminName", "java.lang.String", "Name of admin") };
		MBeanOperationInfo[] dOperations = new MBeanOperationInfo[1];
		dOperations[0] = new MBeanOperationInfo("startServer", "Starts the server", params, "void",
				MBeanOperationInfo.ACTION);
		dAttributes[0] = new MBeanAttributeInfo("startTime", "java.lang.Long", "Time at which server started", true,
				false, false);

		// use reflection to get constructor signatures
		Constructor[] constructors = this.getClass().getConstructors();
		dConstructors[0] = new MBeanConstructorInfo("No-parameter constructor", // description
				constructors[0]); // the contructor object

		MBeanInfo dMBeanInfo = new MBeanInfo(this.getClass().getName(), "DynamicMBean implementation ", dAttributes,
				dConstructors, dOperations, new MBeanNotificationInfo[0]);
		return dMBeanInfo;
	}

	@Override
	public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException {
		// TODO Auto-generated method stub
		return getStartTime();
	}

	@Override
	public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException,
			MBeanException, ReflectionException {
		startTime = (Long) attribute.getValue();

	}

	@Override
	public AttributeList getAttributes(String[] attributes) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public AttributeList setAttributes(AttributeList attributes) {
		// TODO Auto-generated method stub
		return null;
	}

	public long getStartTime() {
		return startTime;
	}

	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}
}
